<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"kuang-yudi.gitee.io","root":"/myblog/","images":"/myblog/images","scheme":"Gemini","version":"8.1.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/myblog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="v-if与v-show的区别、常用修饰符、vue中key属性、vue组件中的data为什么是函数">
<meta property="og:type" content="article">
<meta property="og:title" content="vue语法">
<meta property="og:url" content="http://kuang-yudi.gitee.io/myblog/2021/01/07/vue%E8%AF%AD%E6%B3%95/index.html">
<meta property="og:site_name" content="KY迪的个人博客">
<meta property="og:description" content="v-if与v-show的区别、常用修饰符、vue中key属性、vue组件中的data为什么是函数">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-01-07T10:40:42.000Z">
<meta property="article:modified_time" content="2021-01-07T14:27:16.484Z">
<meta property="article:author" content="匡宇迪">
<meta property="article:tag" content="vue语法">
<meta property="article:tag" content="修饰符">
<meta property="article:tag" content="vue中key属性">
<meta property="article:tag" content="vue组件中的data为函数">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://kuang-yudi.gitee.io/myblog/2021/01/07/vue%E8%AF%AD%E6%B3%95/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>vue语法 | KY迪的个人博客</title>
  



  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">KY迪的个人博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">个人技术博客</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-v-if-%E5%92%8C-v-show-%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">1.</span> <span class="nav-text">1.v-if 和 v-show 的区别</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E7%9B%B8%E5%90%8C%E7%82%B9%EF%BC%9A-%E9%83%BD%E6%98%AF%E5%88%A4%E6%96%AD-DOM-%E8%8A%82%E7%82%B9%E6%98%AF%E5%90%A6%E8%A6%81%E6%98%BE%E7%A4%BA%E3%80%82"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 相同点： 都是判断 DOM 节点是否要显示。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%B8%8D%E5%90%8C%E7%82%B9%EF%BC%9A"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 不同点：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-vue-%E5%B8%B8%E7%94%A8%E7%9A%84%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="nav-number">2.</span> <span class="nav-text">2. vue 常用的修饰符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E4%BA%8B%E4%BB%B6%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 事件修饰符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E6%8C%89%E9%94%AE%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 按键修饰符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E7%B3%BB%E7%BB%9F%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 系统修饰符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-%E8%A1%A8%E5%8D%95%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="nav-number">2.4.</span> <span class="nav-text">2.4 表单修饰符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-%E7%BB%84%E4%BB%B6%E4%B8%AD%E8%87%AA%E5%AE%9A%E4%B9%89%E4%BA%8B%E4%BB%B6%E7%9A%84%E4%BF%AE%E9%A5%B0%E7%AC%A6-sync"><span class="nav-number">2.5.</span> <span class="nav-text">2.5 组件中自定义事件的修饰符 .sync</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-vue-%E4%B8%AD-key-%E5%B1%9E%E6%80%A7%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-number">3.</span> <span class="nav-text">3. vue 中 key 属性的作用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E6%8F%90%E9%AB%98%E6%80%A7%E8%83%BD"><span class="nav-number">3.1.</span> <span class="nav-text">3.1 提高性能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E5%BC%BA%E5%88%B6%E6%9B%BF%E6%8D%A2%E5%85%83%E7%B4%A0-%E7%BB%84%E4%BB%B6%E8%80%8C%E4%B8%8D%E6%98%AF%E7%AE%80%E5%8D%95%E5%A4%8D%E7%94%A8"><span class="nav-number">3.2.</span> <span class="nav-text">3.2 强制替换元素&#x2F;组件而不是简单复用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-Vue-%E7%BB%84%E4%BB%B6%E4%B8%AD-data-%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BF%85%E9%A1%BB%E6%98%AF%E5%87%BD%E6%95%B0%EF%BC%9F"><span class="nav-number">4.</span> <span class="nav-text">4. Vue 组件中 data 为什么必须是函数？</span></a></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="匡宇迪"
      src="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
  <p class="site-author-name" itemprop="name">匡宇迪</p>
  <div class="site-description" itemprop="description">用于记录学习web前端</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">87</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/tonykyd/tonykyd.github.io" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tonykyd&#x2F;tonykyd.github.io" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1303665509@qq.com" title="E-Mail → mailto:1303665509@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://kuang-yudi.gitee.io/myblog/2021/01/07/vue%E8%AF%AD%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
      <meta itemprop="name" content="匡宇迪">
      <meta itemprop="description" content="用于记录学习web前端">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="KY迪的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          vue语法
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-01-07 18:40:42 / 修改时间：22:27:16" itemprop="dateCreated datePublished" datetime="2021-01-07T18:40:42+08:00">2021-01-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/myblog/categories/%E9%9D%A2%E8%AF%95%E9%A2%98/" itemprop="url" rel="index"><span itemprop="name">面试题</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

            <div class="post-description">v-if与v-show的区别、常用修饰符、vue中key属性、vue组件中的data为什么是函数</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="1-v-if-和-v-show-的区别"><a href="#1-v-if-和-v-show-的区别" class="headerlink" title="1.v-if 和 v-show 的区别"></a>1.v-if 和 v-show 的区别</h2><h3 id="1-1-相同点：-都是判断-DOM-节点是否要显示。"><a href="#1-1-相同点：-都是判断-DOM-节点是否要显示。" class="headerlink" title="1.1 相同点： 都是判断 DOM 节点是否要显示。"></a>1.1 相同点： 都是判断 DOM 节点是否要显示。</h3><h3 id="1-2-不同点："><a href="#1-2-不同点：" class="headerlink" title="1.2 不同点："></a>1.2 不同点：</h3><p>a.实现方式：v-if 直接从 Dom 树上删除或重建元素节点。 v-show 只是在修改元素的 css 样式，也就是 display 的属性值，元素始终在 Dom 树上。</p>
<p>b.编译过程：v-if 切换有一个局部编译/卸载的过程，切换过程中合适地销毁和重建内部的事件监听和子组件； v-show 只是简单的基于 css 切换；</p>
<p>c.编译条件：v-if 是惰性的，如果初始条件为假，则什么也不做；只有在条件第一次变为真时才开始局部编译；v-show 是在任何条件下（首次条件是否为真）都被编译，然后被缓存，而且 DOM 元素始终被保留；</p>
<p>d.性能消耗：v-if 有更高的切换消耗，不适合做频繁的切换； v-show 有更高的初始渲染消耗，适合做频繁的切换；</p>
<h2 id="2-vue-常用的修饰符"><a href="#2-vue-常用的修饰符" class="headerlink" title="2. vue 常用的修饰符"></a>2. vue 常用的修饰符</h2><h3 id="2-1-事件修饰符"><a href="#2-1-事件修饰符" class="headerlink" title="2.1 事件修饰符"></a>2.1 事件修饰符</h3><p>① .stop —— 阻止冒泡（通俗讲就是阻止事件向上级 DOM 元素传递）<br>② .prevent —— prevent：阻止默认事件的发生(点击超链接的时候会进行页面的跳转，点击表单提交按钮时会 重新加载页面)<br>③ .capture —— 捕获冒泡，即有冒泡发生时，有该修饰符的 dom 元素会先执行，如果有多个，从外到内依次执行，然后再按自 然顺序执行触发的事件。<br>④ .self —— 将事件绑定到自身，只有自身才能触发，通常用于避免冒泡事件的影响<br>⑤ .once —— 设置事件只能触发一次，比如按钮的点击等。<br>⑥ .passive —— 表示 listener(事件处理函数) 永远不会调用 preventDefault()，所以 passive 和 prevent 冲突，不能同时绑定在一个监听器上。<br><strong>原理：</strong>浏览器无法预先知道一个监听器会不会调用 preventDefault()，它能做的只有等监听器执行完后再去执行默认行为，监听器 listener 执行比较消耗性能，所以.passive 修饰符表示处理事件函数中不会调用 preventDefault 函数，就会减少了额外的监听，从而提高了性能<br>注：<strong>一般用在滚动监听</strong>，@scoll，@touchmove 。因为滚动监听过程中，移动每个像素都会产生一次事件，每次都使用内核线程查询 prevent 会使滑动卡顿。我们通过 passive 将内核线程查询跳过，可以大大提升滑动的流畅度。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 阻止单击事件继续传播 --&gt;</span><br><span class="line">&lt;a v-on:click.stop&#x3D;&quot;doThis&quot;&gt;&lt;&#x2F;a&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- 提交事件不再重载页面 --&gt;</span><br><span class="line">&lt;form v-on:submit.prevent&#x3D;&quot;onSubmit&quot;&gt;&lt;&#x2F;form&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- 修饰符可以串联 --&gt;</span><br><span class="line">&lt;a v-on:click.stop.prevent&#x3D;&quot;doThat&quot;&gt;&lt;&#x2F;a&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- 只有修饰符 --&gt;</span><br><span class="line">&lt;form v-on:submit.prevent&gt;&lt;&#x2F;form&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- 添加事件监听器时使用事件捕获模式 --&gt;</span><br><span class="line">&lt;!-- 即内部元素触发的事件先在此处理，然后才交由内部元素进行处理 --&gt;</span><br><span class="line">&lt;div v-on:click.capture&#x3D;&quot;doThis&quot;&gt;...&lt;&#x2F;div&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- 只当在 event.target 是当前元素自身时触发处理函数 --&gt;</span><br><span class="line">&lt;!-- 即事件不是从内部元素触发的 --&gt;</span><br><span class="line">&lt;div v-on:click.self&#x3D;&quot;doThat&quot;&gt;...&lt;&#x2F;div&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>串联时的顺序</strong><br>修饰符时，顺序很重要；相应的代码会以同样的顺序产生。因此，用 v-on:click.prevent.self 会阻止所有的点击，而 v-on:click.self.prevent 只会阻止对元素自身的点击。</p>
<h3 id="2-2-按键修饰符"><a href="#2-2-按键修饰符" class="headerlink" title="2.2 按键修饰符"></a>2.2 按键修饰符</h3><p>键盘事件后面可以跟按键修饰符或者按键码<br>.enter<br>.tab<br>.delete (捕获“删除”和“退格”键)<br>.esc<br>.space<br>.up<br>.down<br>.left<br>.right</p>
<p>通过全局 config.keyCodes 对象自定义按键修饰符别名：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Vue.config.keyCodes.f1 &#x3D; 112</span><br></pre></td></tr></table></figure>

<h3 id="2-3-系统修饰符"><a href="#2-3-系统修饰符" class="headerlink" title="2.3 系统修饰符"></a>2.3 系统修饰符</h3><p>系统修饰符与按键修饰符不同：按键修饰符触发的同时系统修饰符必须处于按下状态<br>.ctrl<br>.alt<br>.shift<br>.meta<br>例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Alt + C --&gt;</span><br><span class="line">&lt;input v-on:keyup.alt.67&#x3D;&quot;clear&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- Ctrl + Click --&gt;</span><br><span class="line">&lt;div v-on:click.ctrl&#x3D;&quot;doSomething&quot;&gt;Do something&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>

<p><strong>.exact 修饰符</strong><br>允许你控制由精确的系统修饰符组合触发的事件(控制系统修饰符)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 即使 Alt 或 Shift 被一同按下时也会触发 --&gt;</span><br><span class="line">&lt;button v-on:click.ctrl&#x3D;&quot;onClick&quot;&gt;A&lt;&#x2F;button&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- 有且只有 Ctrl 被按下的时候才触发 --&gt;</span><br><span class="line">&lt;button v-on:click.ctrl.exact&#x3D;&quot;onCtrlClick&quot;&gt;A&lt;&#x2F;button&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- 没有任何系统修饰符被按下的时候才触发 --&gt;</span><br><span class="line">&lt;button v-on:click.exact&#x3D;&quot;onClick&quot;&gt;A&lt;&#x2F;button&gt;</span><br></pre></td></tr></table></figure>

<p><strong>鼠标按钮修饰符</strong><br>.left<br>.right<br>.middle</p>
<h3 id="2-4-表单修饰符"><a href="#2-4-表单修饰符" class="headerlink" title="2.4 表单修饰符"></a>2.4 表单修饰符</h3><p><strong>.lazy</strong><br>默认情况下，v-model 在每次 input 事件触发后将输入框的值与数据进行同步 (除了上述输入法组合文字时)。你可以添加 lazy 修饰符，从而转为在 change 事件进行同步：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 在“change”时而非“input”时更新 --&gt;</span><br><span class="line">&lt;input v-model.lazy&#x3D;&quot;msg&quot;&gt;</span><br></pre></td></tr></table></figure>

<p><strong>.number</strong><br>用户的输入值转为数值类型<br><strong>.trim</strong><br>自动过滤用户输入的首尾空白字符</p>
<h3 id="2-5-组件中自定义事件的修饰符-sync"><a href="#2-5-组件中自定义事件的修饰符-sync" class="headerlink" title="2.5 组件中自定义事件的修饰符 .sync"></a>2.5 组件中自定义事件的修饰符 .sync</h3><p>作用：在子组件中修改父组件的值，使数据改动来源更明显</p>
<p>实现给组件的属性动态绑定数据，并监听一个 update:属性的事件名=数据=$event，子组件中用 props 声明此属性，并发射事件名为 update:属性和数据</p>
<p>总结：判断父组件数据的改动：有此属性的子组件更改了父组件的数据。<br>例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;普通写法</span><br><span class="line">&lt;text-document</span><br><span class="line">    v-bind:title&#x3D;&quot;doc.title&quot;</span><br><span class="line">    v-on:update:title&#x3D;&quot;doc.title &#x3D; $event&quot;</span><br><span class="line">&gt;&lt;&#x2F;text-document&gt;</span><br><span class="line">&#x2F;&#x2F;语法糖写法</span><br><span class="line">&lt;text-document</span><br><span class="line">	v-bind:title.sync&#x3D;&quot;doc.title&quot;</span><br><span class="line">&gt;&lt;&#x2F;text-document&gt;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 子组件</span><br><span class="line">Vue.component(&#39;text-document&#39;, &#123;</span><br><span class="line">  &#x2F;&#x2F; 声明属性</span><br><span class="line">  props: [&#39;title&#39;],</span><br><span class="line">  template: &#96;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;button @click&#x3D;&#39;setNewTitle&#39;&gt;更新标题&lt;&#x2F;button&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  &#96;,</span><br><span class="line">  methods:&#123;</span><br><span class="line">   setNewTitle:function()&#123;</span><br><span class="line">       &#x2F;&#x2F;手动进行更新父组件中的值</span><br><span class="line">       this.$emit(&#39;update:title&#39;, &#39;这步操作修改标题实现prop双向绑定&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">var vm &#x3D; new Vue(&#123;</span><br><span class="line">  el:&#39;#app&#39;,</span><br><span class="line">  data:&#123;</span><br><span class="line">     doc:&#123;</span><br><span class="line">      title:&#39;对prop进行“双向绑定”&#39;</span><br><span class="line">     &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h2 id="3-vue-中-key-属性的作用"><a href="#3-vue-中-key-属性的作用" class="headerlink" title="3. vue 中 key 属性的作用"></a>3. vue 中 key 属性的作用</h2><h3 id="3-1-提高性能"><a href="#3-1-提高性能" class="headerlink" title="3.1 提高性能"></a>3.1 提高性能</h3><p>如果不使用 key，那么 vue 在渲染同样的标签元素时默认就地复用策略<br>Vue 会通过改变原来元素的内容和增加/减少元素来匹配改变后的数据，因为没有 key 属性，Vue 无法跟踪每个节点，只能通过这样的方法来完成变更。</p>
<p>在有了 key 属性之后，Vue 会记住元素们的顺序，并根据这个顺序在适当的位置插入/删除元素来完成更新，这种方法比没有 key 属性时的就地复用策略效率更高。</p>
<p>总结：使用 key 来给每个节点做一个唯一标识,管理可复用的元素，减少不必要的元素的重新渲染,也渲染了必要的元素。key 的作用主要是为了高效的更新虚拟 DOM。</p>
<h3 id="3-2-强制替换元素-组件而不是简单复用"><a href="#3-2-强制替换元素-组件而不是简单复用" class="headerlink" title="3.2 强制替换元素/组件而不是简单复用"></a>3.2 强制替换元素/组件而不是简单复用</h3><p><strong>使用场景 1</strong>：触发过渡(创建新标签元素)<br>注：Vue 在插入、更新或者移除 DOM 时，提供多种不同方式的应用过渡效果(即想要触发过渡，渲染的元素必须是新的，而不是复用的)<br>vue 中在使用相同标签名的元素进行过渡切换时，也会使用到 key 属性，其目的也是为了让 vue 可以区分它们，因此在 key 值改变时创建了新元素，从而触发过渡<br><strong>使用场景 2</strong>：触发生命周期函数(创建新组件)<br>组件绑定 key 值，key 值改变，组件进行销毁、创建，触发对应生命周期函数</p>
<h2 id="4-Vue-组件中-data-为什么必须是函数？"><a href="#4-Vue-组件中-data-为什么必须是函数？" class="headerlink" title="4. Vue 组件中 data 为什么必须是函数？"></a>4. Vue 组件中 data 为什么必须是函数？</h2><p>注册组件时，传入的 data 属性值如果是个对象，那么所有注册的组件都有同样的 data 对象(堆内存地址相同)，会互相影响。</p>
<p>如果把 data 指向一个函数，则每个 data 都有自己的作用域(即有自己的堆地址)，每个注册的组件中的 data 就互不干扰了</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
        <a target="_blank" class="social-link" href="/myblog/images/wechat_channel.jpg">
          <span class="icon">
            <i class="fab fa-weixin"></i>
          </span>

          <span class="label">WeChat</span>
        </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/myblog/tags/vue%E8%AF%AD%E6%B3%95/" rel="tag"># vue语法</a>
              <a href="/myblog/tags/%E4%BF%AE%E9%A5%B0%E7%AC%A6/" rel="tag"># 修饰符</a>
              <a href="/myblog/tags/vue%E4%B8%ADkey%E5%B1%9E%E6%80%A7/" rel="tag"># vue中key属性</a>
              <a href="/myblog/tags/vue%E7%BB%84%E4%BB%B6%E4%B8%AD%E7%9A%84data%E4%B8%BA%E5%87%BD%E6%95%B0/" rel="tag"># vue组件中的data为函数</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/myblog/2021/01/07/vue%E9%9D%A2%E8%AF%95%E9%A2%981/" rel="prev" title="vue面试题1">
                  <i class="fa fa-chevron-left"></i> vue面试题1
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/myblog/2021/01/08/vue%E7%BB%84%E4%BB%B6/" rel="next" title="vue组件">
                  vue组件 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">匡宇迪</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/myblog/js/utils.js"></script><script src="/myblog/js/motion.js"></script><script src="/myblog/js/next-boot.js"></script>

  
<script src="/myblog/js/local-search.js"></script>






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.css">

<script>
NexT.utils.loadComments('#gitalk-container', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '1926f8a1c453a1121d8d',
      clientSecret: '426722f9eef7599f8ebb3220379e704ea007a5f2',
      repo        : '',
      owner       : '',
      admin       : [''],
      id          : '00c58db38d09ac58f2f265a8c0d562c2',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

</body>
</html>
